home *** CD-ROM | disk | FTP | other *** search
/ Die Speccy' 97 / Die Speccy' 97.iso / amiga_system / the_aminet / comm / bbs / s342q07.lha / nlstat.c < prev    next >
C/C++ Source or Header  |  1994-11-15  |  12KB  |  347 lines

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <math.h>
  4. #include <stdlib.h>
  5. #define UP "System brought up"
  6. #define DOWN "System brought down"
  7. #define NET "System in network mode"
  8. #define NOLOGIN "<No Login>"
  9. #define BAUD300 "300)"
  10. #define BAUD1200 "1200)"
  11. #define BAUD2400 "2400)"
  12. #define BAUD4800 "4800)"
  13. #define BAUD9600 "9600)"
  14. #define BAUD14400 "14400)"
  15. #define BAUD19200 "19200)"
  16. #define CONSOLE "sysConsole)"
  17. #define DEFAULT_READ "calllog.sys"
  18. #define DEFAULT_WRITE "dumbstats"
  19. int main (int , char **);
  20. int main (int argc, char *argv[])
  21.   {
  22.   long calls, net, timeout, error, badword, chat;
  23.   long nologin, restart, calltime[96], nettime[96], tottime[96];
  24.   long duration[1440], baud[8], i, j, k, l, longest, lasthour, atime;
  25.   long lastmin, h1, h2, m1, m2, newuser = 0, stay = 0, busytime[96];
  26.   char firstdate[10], firsttime[10], buffer[80], *name, *date, *starthour,
  27.   *startmin, *stophour, *stopmin, *other, *readname, *writename;
  28.   char lasttime[10],lastdate[10];
  29.   FILE *readfile, *writefile;
  30.  
  31.   calls = net = timeout = error = badword = chat = nologin = 0;
  32.   restart = longest = atime = newuser = stay = 0;
  33.  
  34.   printf ("Citadel log file cruncher Version 1.01\n");
  35.   printf ("  by Lobotosoft\n");
  36.   for (i = 0; i < 96; ++i)
  37.     {
  38.     calltime[i] = 0;
  39.     nettime[i] = 0;
  40.     tottime[i] = 0;
  41.     busytime[i] = 0;
  42.  
  43.     }
  44.   for (i = 0; i < 8; ++i)     baud[i] = 0;
  45.   for (i = 0; i < 1440; ++i)  duration[i] = 0;
  46.   writename = DEFAULT_WRITE;
  47.   readname  = DEFAULT_READ;
  48.   if (argc > 2)  readname = argv[2];
  49.   if (argc > 1)  writename = argv[1];
  50.   readfile = fopen (readname, "r");
  51.   if (readfile == NULL)
  52.     {
  53.     printf ("Unable to open log file [%s] for input!\n", readname);
  54.     printf ("Usage: logcrunch [output file] [input file]\n");
  55.     exit (0);
  56.  
  57.     }
  58.   writefile = fopen (writename, "w");
  59.   if (writefile == NULL)
  60.     {
  61.     printf ("Unable to open stats file [%s] for output!\n", writename);
  62.     printf ("Usage: logcrunch [output file] [input file]\n");
  63.     fclose (readfile);
  64.     exit (0);
  65.  
  66.     }
  67.   name      = buffer;
  68.   date      = &buffer[24];
  69.   starthour = &buffer[32];
  70.   startmin  = &buffer[35];
  71.   stophour  = &buffer[40];
  72.   stopmin   = &buffer[43];
  73.   other     = &buffer[47];
  74.   fgets (buffer, 80, readfile);
  75.   while (!feof (readfile))
  76.     {
  77.     if (strncmp (buffer, UP, strlen (UP)) == 0) ++restart;
  78.     if (strlen (buffer) > 23  &&  buffer[22] == ':')
  79.       {
  80.       buffer[22] = 0;
  81.       buffer[31] = 0;
  82.       buffer[37] = 0;
  83.       buffer[42] = 0;
  84.       buffer[45] = 0;
  85.       strcpy (lasttime, starthour);
  86.       strcpy (lastdate, date);
  87.       if (calls == 0  &&  net == 0)
  88.         {
  89.         strcpy (firsttime, starthour);
  90.         strcpy (firstdate, date);
  91.         buffer[34] = 0;
  92.         lasthour = atoi (starthour);
  93.         lastmin = atoi (startmin);
  94.  
  95.         }
  96.       buffer[34] = 0;
  97.       h1 = atoi (starthour);
  98.       h2 = atoi (stophour);
  99.       m1 = atoi (startmin);
  100.       m2 = atoi (stopmin);
  101.       i = h1 * 4 + m1 / 15;
  102.       j = h2 * 4 + m2 / 15;
  103.       k = lasthour * 4 + lastmin / 15;
  104.       tottime[k] += (k + 1) * 15 - lasthour * 60 - lastmin;
  105.       if (j < k)
  106.         {
  107.         for (l = k + 1; l < 96; ++l) tottime[l] += 15;
  108.         for (l = 0; l < j + 1; ++l)  tottime[l] += 15;
  109.         tottime[j] -= (j + 1) * 15 - h2 * 60 - m2;
  110.  
  111.         }
  112.       else
  113.         {
  114.         for (l = k + 1; l < j + 1; ++l) tottime[l] += 15;
  115.         tottime[j] -= (j + 1) * 15 - h2 * 60 - m2;
  116.  
  117.         }
  118.       lasthour = h2;
  119.       lastmin = m2;
  120.       if (strncmp (name, NET, strlen (NET)) == 0)
  121.         {
  122.         ++net;
  123.         ++nettime[i];
  124.         busytime[i] += (i + 1) * 15 - h1 * 60 - m1;
  125.         if (j < i)
  126.           {
  127.           for (l = i + 1; l < 96; ++l) busytime[l] += 15;
  128.           for (l = 0; l < j + 1; ++l)  busytime[l] += 15;
  129.           busytime[j] -= (j + 1) * 15 - h2 * 60 - m2;
  130.  
  131.           }
  132.         else
  133.           {
  134.           for (l = i + 1; l < j + 1; ++l) busytime[l] += 15;
  135.           busytime[j] -= (j + 1) * 15 - h2 * 60 - m2;
  136.  
  137.           }
  138.  
  139.         }
  140.       else
  141.         {
  142.         ++calls;
  143.         ++calltime[i];
  144.         busytime[i] += (i + 1) * 15 - h1 * 60 - m1;
  145.         if (j < i)
  146.           {
  147.           for (l = i + 1; l < 96; ++l) busytime[l] += 15;
  148.           for (l = 0; l < j + 1; ++l)  busytime[l] += 15;
  149.           busytime[j] -= (j + 1) * 15 - h2 * 60 - m2;
  150.  
  151.           }
  152.         else
  153.           {
  154.           for (l = i + 1; l < j + 1; ++l) busytime[l] += 15;
  155.           busytime[j] -= (j + 1) * 15 - h2 * 60 - m2;
  156.  
  157.           }
  158.         l = (h2 - h1) * 60 + m2 - m1;
  159.         if (l < 0)
  160.         l += 1440;
  161.         ++duration[l];
  162.         if (l > longest) longest = l;
  163.         atime += l;
  164.         if (strncmp (NOLOGIN,    name, strlen (NOLOGIN))  == 0) ++nologin;
  165.         if (strncmp (CONSOLE,   other, strlen (CONSOLE))  == 0)++baud[0];
  166.         if (strncmp (BAUD300,   other, strlen (BAUD300))  == 0)++baud[1];
  167.         if (strncmp (BAUD1200,  other, strlen (BAUD1200)) == 0)++baud[2];
  168.         if (strncmp (BAUD2400,  other, strlen (BAUD2400)) == 0)++baud[3];
  169.         if (strncmp (BAUD4800,  other, strlen (BAUD4800)) == 0)++baud[4];
  170.         if (strncmp (BAUD9600,  other, strlen (BAUD9600)) == 0)++baud[5];
  171.         if (strncmp (BAUD14400, other, strlen (BAUD14400))== 0)++baud[6];
  172.         if (strncmp (BAUD19200, other, strlen (BAUD19200))== 0)++baud[7];
  173.         for (l = 0; other[l] != ')'  &&  other[l] != 0; ++l);
  174.         for (;other[l] != 0; ++l)
  175.           {
  176.           if (other[l] == '+')
  177.              ++newuser;
  178.           else if (other[l] == '-')
  179.              ++stay;
  180.           else if (other[l] == 't'  ||  other[l] == 'T')
  181.              ++timeout;
  182.           else if (other[l] == 'e'  ||  other[l] == 'E')
  183.              ++error;
  184.           else if (other[l] == 'b'  ||  other[l] == 'B')
  185.              ++badword;
  186.           else if (other[l] == 'c'  ||  other[l] == 'C')
  187.              ++chat;
  188.  
  189.           }
  190.  
  191.         }
  192.  
  193.       }
  194.     fgets (buffer, 80, readfile);
  195.     printf ("%ld calls, %ld net sessions\r", calls, net);
  196.  
  197.     }
  198.   printf ("\nCrunching data....\n");
  199.   fprintf (writefile, "        System Usage Data\n \n");
  200.   fprintf (writefile, "  Based on usage from  %s, %s to %s, %s\n\n"
  201.   , firstdate,firsttime,lastdate,lasttime);
  202.   fprintf (writefile, "        SYSTEM CALL DATA: \n\n");
  203.   fprintf (writefile, " There were %ld restarts during this period.\n", restart);
  204.   fprintf (writefile, " There were %ld calls, and %ld network sessions\n", calls, net);
  205.   fprintf (writefile, "                     Number   Percent\n");
  206.   fprintf (writefile, " -------------------------------------\n");
  207.   fprintf (writefile, " New Users            %5ld     %3ld\n", newuser, 100 * newuser / calls);
  208.   fprintf (writefile, " No Login Calls       %5ld     %3ld\n", nologin, 100 * nologin / calls);
  209.   fprintf (writefile, " .Terminate Stay      %5ld     %3ld\n", stay,    100 * stay / calls);
  210.   fprintf (writefile, " Chat Attempts        %5ld     %3ld\n", chat,    100 * chat / calls);
  211.   fprintf (writefile, " Timeouts             %5ld     %3ld\n", timeout, 100 * timeout / calls);
  212.   fprintf (writefile, " Exceeded Error Limit %5ld     %3ld\n", error,   100 * error / calls);
  213.   fprintf (writefile, " Badwords usage       %5ld     %3ld\n", badword, 100 * badword / calls);
  214.   fprintf (writefile, " \n    Caller Baud Rates:\n");
  215.   if (baud[1] > 0) fprintf (writefile, "   300 Baud           %5ld     %3ld\n", baud[1], 100 * baud[1] / calls);
  216.   if (baud[2] > 0) fprintf (writefile, "  1200 Baud           %5ld     %3ld\n", baud[2], 100 * baud[2] / calls);
  217.   if (baud[3] > 0) fprintf (writefile, "  2400 Baud           %5ld     %3ld\n", baud[3], 100 * baud[3] / calls);
  218.   if (baud[4] > 0) fprintf (writefile, "  4800 Baud           %5ld     %3ld\n", baud[4], 100 * baud[4] / calls);
  219.   if (baud[5] > 0) fprintf (writefile, "  9600 Baud           %5ld     %3ld\n", baud[5], 100 * baud[5] / calls);
  220.   if (baud[6] > 0) fprintf (writefile, " 14400 Baud           %5ld     %3ld\n", baud[6], 100 * baud[6] / calls);
  221.   if (baud[7] > 0) fprintf (writefile, " 19200 Baud           %5ld     %3ld\n", baud[6], 100 * baud[6] / calls);
  222.   if (baud[0] > 0) fprintf (writefile, " sysConsole           %5ld     %3ld\n", baud[0], 100 * baud[0] / calls);
  223.   fprintf (writefile, " \n \n   CALL DURATION DATA\n \n");
  224.   fprintf (writefile, "    The longest call was %ld minutes,", longest);
  225.   fprintf (writefile, " The average call was %ld minutes.\n \n", atime /  calls);
  226.   for (k = 0, i = 0; k < calls - calls / 25; ++i)  k += duration[i];
  227.   h1 = calls - k;
  228.   longest = i;
  229.   i = i / 50 + 1;
  230.   for (k = 0, m2 = h1; k < longest; ++k)
  231.     {
  232.     for (l = k * i, m1 = 0; l < (k + 1) * i; ++l) m1 += duration[l];
  233.     duration[k] = m1;
  234.     if (m1 > m2) m2 = m1;
  235.  
  236.     }
  237.   j = m2 / 60 + 1;
  238.   fprintf (writefile, " Call duration histogram:\n");
  239.   fprintf (writefile, "  Each bar represents the number of calls of that\n");
  240.   fprintf (writefile, "  duration or up to %ld minutes longer.", i);
  241.   fprintf (writefile, "  Each '=' represents %ld calls.\n \n", j);
  242.   for (k = 0; longest >= k * i; ++k)
  243.     {
  244.     fprintf (writefile, "  %4ld", k * i);
  245.     for (l = 0; l < duration[k] / j; ++l) fprintf (writefile, "=");
  246.     fprintf (writefile, " (%ld)\n", duration[k]);
  247.  
  248.     }
  249.   fprintf (writefile, " >%4ld", --k * i);
  250.   for (l = 0; l < h1 / j; ++l)
  251.   fprintf (writefile, "=");
  252.   fprintf (writefile, " (%ld)\n", h1);
  253.   fprintf (writefile, " \n \n   CALL TIMING DATA\n \n");
  254.   i = 4;
  255.   m1 = 0;
  256.   m2 = 0;
  257.   for (k = 0; k < 24; ++k)
  258.     {
  259.     h1 = 0;
  260.     h2 = 0;
  261.     for (l = k * i; l < (k + 1) * i; ++l)
  262.       {
  263.       h1 += calltime[l];
  264.       h2 += nettime[l];
  265.  
  266.       }
  267.     calltime[k] = h1;
  268.     nettime[k] = h2;
  269.     if (h1 > m1)    m1 = h1;
  270.     if (h2 > m2)    m2 = h2;
  271.  
  272.     }
  273.   h1 = m1 / 60 + 1;
  274.   h2 = m2 / 60 + 1;
  275.   fprintf (writefile, " Call timing histogram:\n");
  276.   fprintf (writefile, "  Each bar represents the number of calls\n");
  277.   fprintf (writefile, "  during that hour.");
  278.   fprintf (writefile, "  Each '=' represents %ld calls.\n", h1);
  279.   for (k = 0; k < 24; ++k)
  280.     {
  281.     fprintf (writefile, " %2ld:00", k);
  282.     for (l = 0; l < calltime[k] / h1; ++l) fprintf (writefile, "=");
  283.     fprintf (writefile, " (%ld)\n", calltime[k]);
  284.  
  285.     }
  286.   fprintf (writefile, "\n Net-Session timing histogram:\n");
  287.   fprintf (writefile, "  Each bar represents the number of net-sessions\n");
  288.   fprintf (writefile, "  during that hour.");
  289.   fprintf (writefile, "  Each '=' represents %ld net-sessions.\n", h2);
  290.   for (k = 0; k < 24; ++k)
  291.     {
  292.     fprintf (writefile, " %2ld:00", k);
  293.     for (l = 0; l < nettime[k] / h2; ++l) fprintf (writefile, "=");
  294.     fprintf (writefile, " (%ld)\n", nettime[k]);
  295.  
  296.     }
  297.   fprintf (writefile, "\n   BEST TIME TO CALL DATA \n");
  298.   for (i = 0, m1 = 0, j = 0; i < 96; ++i)
  299.     {
  300.     if (tottime[i] == 0)
  301.       {
  302.       fprintf (writefile, "  Best time to call data is unavailable,");
  303.       fprintf (writefile, " since there is less than 24 hours of data.\n");
  304.       fclose (writefile);
  305.       fclose (readfile);
  306.       exit (0);
  307.  
  308.       }
  309.     if ((tottime[i] - busytime[i]) * 100 / tottime[i] > m1)
  310.       {
  311.       m1 = (tottime[i] - busytime[i]) * 100 / tottime[i];
  312.       j = i;
  313.  
  314.       }
  315.  
  316.     }
  317.   fprintf (writefile, " The best time to call is around %ld:%02ld,\n", j / 4, (j % 4) * 15);
  318.   fprintf (writefile, "  when there is a %ld percent chance of connecting.\n",  m1);
  319.   i = 4;
  320.   for (m1 = 0, j = 0; j < 24; ++j)
  321.     {
  322.     for (k = j * i, h1 = 0, h2 = 0; k < (j + 1) * i; ++k)
  323.       {
  324.       h1 += tottime[k];
  325.       h2 += busytime[k];
  326.  
  327.       }
  328.     busytime[j] = (h1 - h2) * 100 / h1;
  329.     if (busytime[j] > m1) m1 = busytime[j];
  330.  
  331.     }
  332.   j = m1 / 60 + 1;
  333.   fprintf (writefile, " \n Best Time to Call Graph:\n");
  334.   fprintf (writefile, "  Each bar represents the chance of connecting\n");
  335.   fprintf (writefile, "   during that hour.\n \n");
  336.   for (k = 0; k < 24; ++k)
  337.     {
  338.     fprintf (writefile, " %2ld:00", k);
  339.     for (l = 0; l < busytime[k] / j; ++l)    fprintf (writefile, "=");
  340.     fprintf (writefile, " %ld\n", busytime[k]);
  341.  
  342.     }
  343.   fclose (readfile);
  344.   fclose (writefile);
  345.   return 0;
  346.   }
  347.